﻿namespace OleDbDAL.Company
{
    using DBUtility;
    using IDal.Company;
    using Model.Company;
    using System;
    using System.Data;
    using System.Text;

    public class CompayAccountApp : ICompayAccount
    {
        private string dbname = "Systems";

        public void Add(CompayAccountEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_CompayAccount(");
            builder.Append("ChainNum,AccountName,AccountMoney,AccountType,BankType,AccountNum,OrderNum,LastUpdateTime,IsUse");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.AccountName + "',");
            builder.Append("" + model.AccountMoney + ",");
            builder.Append("" + model.AccountType + ",");
            builder.Append("" + model.BankType + ",");
            builder.Append("'" + model.AccountNum + "',");
            builder.Append("" + model.OrderNum + ",");
            builder.Append("'" + model.LastUpdateTime + "',");
            builder.Append("" + model.IsUse + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int AccountId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_CompayAccount ");
            builder.Append(" where AccountId=" + AccountId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_CompayAccount ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by AccountId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "AccountId", "T_CompayAccount", swhere);
        }

        public CompayAccountEnt GetModel(int AccountId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select AccountId,ChainNum,AccountName,AccountMoney,AccountType,BankType,AccountNum,OrderNum,LastUpdateTime,IsUse from T_CompayAccount ");
            builder.Append(" where AccountId=" + AccountId);
            CompayAccountEnt ent = new CompayAccountEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.AccountId = AccountId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.AccountName = table.Rows[0]["AccountName"].ToString();
                if (table.Rows[0]["AccountMoney"].ToString() != "")
                {
                    ent.AccountMoney = decimal.Parse(table.Rows[0]["AccountMoney"].ToString());
                }
                if (table.Rows[0]["AccountType"].ToString() != "")
                {
                    ent.AccountType = int.Parse(table.Rows[0]["AccountType"].ToString());
                }
                if (table.Rows[0]["BankType"].ToString() != "")
                {
                    ent.BankType = int.Parse(table.Rows[0]["BankType"].ToString());
                }
                ent.AccountNum = table.Rows[0]["AccountNum"].ToString();
                if (table.Rows[0]["OrderNum"].ToString() != "")
                {
                    ent.OrderNum = int.Parse(table.Rows[0]["OrderNum"].ToString());
                }
                if (table.Rows[0]["LastUpdateTime"].ToString() != "")
                {
                    ent.LastUpdateTime = DateTime.Parse(table.Rows[0]["LastUpdateTime"].ToString());
                }
                if (table.Rows[0]["IsUse"].ToString() != "")
                {
                    ent.IsUse = int.Parse(table.Rows[0]["IsUse"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_CompayAccount ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "AccountId");
        }

        public void Update(CompayAccountEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_CompayAccount set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("AccountName='" + model.AccountName + "',");
            builder.Append("AccountMoney=" + model.AccountMoney + ",");
            builder.Append("AccountType=" + model.AccountType + ",");
            builder.Append("BankType=" + model.BankType + ",");
            builder.Append("AccountNum='" + model.AccountNum + "',");
            builder.Append("OrderNum=" + model.OrderNum + ",");
            builder.Append("LastUpdateTime='" + model.LastUpdateTime + "',");
            builder.Append("IsUse=" + model.IsUse + "");
            builder.Append(" where AccountId=" + model.AccountId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

